package org.longteng.mobileluck.algorithm.dao.impl;


import lombok.extern.slf4j.Slf4j;
import org.longteng.mobileluck.algorithm.bean.ChinesStrokesFortuneTable;
import org.longteng.mobileluck.algorithm.dao.ChinesStrokesFortuneDao;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * 姓名笔画数吉祥如意表数据读入csv实现
 * @author hymn.com
 */
@Slf4j
@Service
public class ChinesStrokesFortuneDaoImpl extends ParserCsvBaseDao implements ChinesStrokesFortuneDao {
    /**姓名笔画数吉祥如意表*/
    private final Map<Integer, ChinesStrokesFortuneTable> chinesStrokesFortuneMap;

    /**
     * 构造是读入csv姓名笔画数吉祥如意表
     */
    public ChinesStrokesFortuneDaoImpl() {
        List<ChinesStrokesFortuneTable> chinesStrokesFortuneList = this.readTableCsv("/table/chinesstrokes_fortune.csv");
        if (chinesStrokesFortuneList == null) {
            log.warn("读入csv【姓名笔画数】吉祥如意运势表为空无法初始化【姓名笔画数】吉祥如意运势表映射。");
            chinesStrokesFortuneList = new ArrayList<>();
        }

        this.chinesStrokesFortuneMap = chinesStrokesFortuneList.stream().collect(Collectors.toMap(ChinesStrokesFortuneTable::getStrokes, Function.identity(), (key1, key2) -> key2));
        if (log.isDebugEnabled()) {
            log.debug("读入csv【姓名笔画数】吉祥如意运势表:\n ->{}", chinesStrokesFortuneMap);
        }
    }

    /**
     * 根据姓名笔画数取，吉祥如意表信息
     * @param chinesStrokes 姓名笔画数
     * @return
     */
    @Override
    public ChinesStrokesFortuneTable getAuspiciousEntity(int chinesStrokes) {
        return this.chinesStrokesFortuneMap.get(chinesStrokes);
    }

    /**
     * 实现基类，获取泛型T的类型 T.class
     * @return
     */
    @Override
    public Class getTClass() {
        return ChinesStrokesFortuneTable.class;
    }
}
